package com.lagou.zerocopy;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class ServerSocket {

    public static void main(String[] args) throws Exception {

        java.net.ServerSocket serverSocket = new java.net.ServerSocket();
        serverSocket.bind(new InetSocketAddress(6688));

        String fileName = "Python-2.7-" + System.currentTimeMillis() + ".tgz";

        FileOutputStream outputStream = new FileOutputStream(fileName);

        while (true) {
            Socket accept = serverSocket.accept();

            InputStream inputStream = accept.getInputStream();

            long startTime = System.currentTimeMillis();

            byte[] bytes = new byte[1024];

            int readLen = 0;

            int total = 0;

            while ((readLen = inputStream.read(bytes)) != -1) {
                total += readLen;
                outputStream.write(bytes);
                outputStream.flush();
            }

            long endTime = System.currentTimeMillis();

            System.out.println("服务端用时：" + (endTime - startTime) + ", 字节总长度:" + total);

            inputStream.close();
            outputStream.close();
            accept.close();
        }


    }
}
